package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimail.product.dao.AttrGroupDao;
import com.atguigu.gulimail.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimail.product.entity.AttrGroupEntity;
import com.atguigu.gulimail.product.entity.CategoryEntity;
import com.atguigu.gulimail.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimail.product.service.CategoryService;
import com.atguigu.gulimail.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.AttrDao;
import com.atguigu.gulimail.product.entity.AttrEntity;
import com.atguigu.gulimail.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import static com.atguigu.common.constant.AttrType.ATTR_TYPE_BASE;
import static com.atguigu.common.constant.AttrType.ATTR_TYPE_SALE;
import static java.util.stream.Collectors.toList;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    CategoryService categoryService;
    @Autowired
    AttrGroupDao attrGroupDao;
    @Autowired
    AttrAttrgroupRelationDao relationDao;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params, String attrType, Long catelogId) {

        final QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if (attrType.equals(ATTR_TYPE_SALE.getMsg())) {
            wrapper.eq("attr_type", ATTR_TYPE_SALE.getCode());
        } else if (attrType.equals(ATTR_TYPE_BASE.getMsg())) {
            wrapper.eq("attr_type", ATTR_TYPE_BASE.getCode());
        }
        if (!StringUtils.isEmpty(params.get("key"))) {
            String keyStr = params.get("key").toString();
            wrapper.and(w -> {
                w.eq("attr_id", keyStr).or().like("attr_name", keyStr);
            });
        }
        if (null != catelogId && catelogId > 0) {
            wrapper.eq("catelog_id", catelogId);
        }
        final IPage<AttrEntity> page1 = new Query<AttrEntity>().getPage(params);
        IPage<AttrEntity> page = this.page(
                page1, wrapper
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void save(AttrVo attr) {
        AttrEntity entity = new AttrEntity();
        BeanUtils.copyProperties(attr, entity);
        this.save(entity);

        if (ATTR_TYPE_BASE.getCode() == attr.getAttrType() && attr.getAttrGroupId() != null
                && attr.getAttrGroupId() > 0
        ) {
            AttrAttrgroupRelationEntity relationEntity = new
                    AttrAttrgroupRelationEntity();
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            relationEntity.setAttrId(entity.getAttrId());
            relationService.save(relationEntity);
        }
    }

    @Override
    public void updateById(AttrVo attr) {
        AttrEntity entity = new AttrEntity();
        BeanUtils.copyProperties(attr, entity);
        this.updateById(entity);

        int count =
                relationService.count(new QueryWrapper<AttrAttrgroupRelationEntity>()
                        .eq("attr_id", entity.getAttrId()));
        if (count > 0) {
            relationService.remove(new QueryWrapper<AttrAttrgroupRelationEntity>()
                    .eq("attr_id", entity.getAttrId()));
        }

        if (ATTR_TYPE_BASE.getCode() == attr.getAttrType()) {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            relationEntity.setAttrId(entity.getAttrId());
            relationService.save(relationEntity);
        }
    }

    @Override
    public AttrVo getAttrVo(AttrEntity attr) {
        AttrVo attrVo = new AttrVo();
        BeanUtils.copyProperties(attr, attrVo);
        if (attr.getCatelogId() != null) {
            final Long[] catelogPath = categoryService.getCatelogPath(attr.getCatelogId());
            attrVo.setCatelogPath(catelogPath);
            final CategoryEntity categoryEntity = categoryService.getById(attr.getCatelogId());
            attrVo.setCatelogName(categoryEntity.getName());
        }
        if (attr.getAttrType() == ATTR_TYPE_BASE.getCode()) {
            final AttrAttrgroupRelationEntity relationEntity =
                    relationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>()
                            .eq("attr_id", attr.getAttrId()));
            if (null != relationEntity) {
                attrVo.setAttrGroupId(relationEntity.getAttrGroupId());
                final AttrGroupEntity attrGroupEntity =
                        attrGroupDao.selectById(relationEntity.getAttrGroupId());
                attrVo.setAttrGroupName(attrGroupEntity.getAttrGroupName());
            }
        }
        return attrVo;
    }

    @Override
    public void removeByIds(Long[] attrIds) {
        this.removeByIds(Arrays.asList(attrIds));
        relationService.remove
                (new QueryWrapper<AttrAttrgroupRelationEntity>()
                        .in("attr_id", attrIds));
    }

    @Override
    public PageUtils unbindList(Long catelogId, Map<String, Object> params) {

        final IPage<AttrEntity> page1 =
                new Query<AttrEntity>()
                        .getPage(params);

        final List<AttrGroupEntity> attrGroupEntities =
                attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().
                        eq("catelog_id", catelogId));
        final List<Long> attrGroupIds =
                attrGroupEntities.stream().map(AttrGroupEntity::getAttrGroupId).collect(toList());
        //not in 集合.

        final List<AttrAttrgroupRelationEntity> relationEntities =
                relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>()
                        .in("attr_group_id", attrGroupIds));
        final List<Long> relationAttrIds =
                relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(toList());

        //相同产品分类下,所有 attr  这个与 attrGroup 的关系.
        final QueryWrapper<AttrEntity> wrapper =
                new QueryWrapper<AttrEntity>()
                        .eq("catelog_id", catelogId)
                        .notIn("attr_id", relationAttrIds);
        IPage<AttrEntity> page = this.page(page1, wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<Long> selectSearchAttrIds(List<Long> baseAttrIds) {
        List<Long> searchAttrIds = new ArrayList<>();
        for (Long baseAttrId : baseAttrIds) {
            final AttrEntity attrEntity = this.getById(baseAttrId);
            if (attrEntity.getSearchType() == 1) {
                searchAttrIds.add(baseAttrId);
            }
        }
        return searchAttrIds;
    }


}